package com.xoa.service.rms.impl;

import com.xoa.dao.rms.RmsRollMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.rms.RmsRollWithBLOBs;
import com.xoa.model.users.Users;
import com.xoa.service.rms.RmsRollService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/25.
 */
@Service
public class RmsRollServiceImpl implements RmsRollService {

    @Autowired
    private RmsRollMapper rmsRollMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public ToJson<RmsRollWithBLOBs> save(RmsRollWithBLOBs rmsRoll, HttpServletRequest request) {
        ToJson<RmsRollWithBLOBs> json = new ToJson<RmsRollWithBLOBs>();
        try{
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
            rmsRoll.setAddUser(user.getUserId());
            rmsRoll.setAddTime(new Date());
            if(StringUtils.checkNull(rmsRoll.getManageUser())){
                rmsRoll.setManageUser("");
            }
            if(StringUtils.checkNull(rmsRoll.getReadUser())){
                rmsRoll.setReadUser("");
            }
            rmsRollMapper.insertSelective(rmsRoll);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    @Override
    public ToJson<RmsRollWithBLOBs> update(RmsRollWithBLOBs rmsRollWithBLOBs, HttpServletRequest request) {
        ToJson<RmsRollWithBLOBs> json = new ToJson<RmsRollWithBLOBs>();
        try{
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
            rmsRollWithBLOBs.setModUser(user.getUserId());
            rmsRollWithBLOBs.setModTime(new Date());

            rmsRollMapper.updateByPrimaryKeySelective(rmsRollWithBLOBs);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    @Override
    public ToJson<RmsRollWithBLOBs> destroy(String rollIds, HttpServletRequest request) {
        ToJson<RmsRollWithBLOBs> json = new ToJson<RmsRollWithBLOBs>();
        try{
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
            String[] split = rollIds.split(",");
            rmsRollMapper.deleteByRollIds(split);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    @Override
    public ToJson<RmsRollWithBLOBs> query(RmsRollWithBLOBs rmsRollWithBLOBs, PageParams pageParams) {
        ToJson<RmsRollWithBLOBs> json = new ToJson<RmsRollWithBLOBs>();
        try{
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("page",pageParams);
            map.put("rollCode",rmsRollWithBLOBs.getRollCode());
            map.put("roomId",rmsRollWithBLOBs.getRoomId());

            map.put("beginDate",rmsRollWithBLOBs.getBeginDate());
            map.put("beginDate2",rmsRollWithBLOBs.getBeginDate2());
            map.put("endDate",rmsRollWithBLOBs.getEndDate());
            map.put("endDate2",rmsRollWithBLOBs.getEndDate2());
            map.put("deadBeginDate",rmsRollWithBLOBs.getDeadBeginDate());
            map.put("deadEndDate",rmsRollWithBLOBs.getDeadEndDate());

            map.put("categoryNo",rmsRollWithBLOBs.getCategoryNo());
            map.put("archiveNo",rmsRollWithBLOBs.getArchiveNo());
            // 缩微号
            map.put("microNo",rmsRollWithBLOBs.getMicroNo());
            // 凭证编号开始
            map.put("certificateStart",rmsRollWithBLOBs.getCertificateStart());
            map.put("certificateStart2",rmsRollWithBLOBs.getCertificateStart2());
            // 凭证编号截止
            map.put("certificateEnd",rmsRollWithBLOBs.getCertificateEnd());
            map.put("certificateEnd2",rmsRollWithBLOBs.getCertificateEnd2());

            // 页数查询
            map.put("rollPage",rmsRollWithBLOBs.getRollPage());
            map.put("rollPage2",rmsRollWithBLOBs.getRollPage2());

            map.put("remark",rmsRollWithBLOBs.getRemark());
            map.put("rollName",rmsRollWithBLOBs.getRollName());
            map.put("years",rmsRollWithBLOBs.getYears());
            map.put("secret",rmsRollWithBLOBs.getSecret());
            map.put("catalogNo",rmsRollWithBLOBs.getCatalogNo());
            map.put("boxNo",rmsRollWithBLOBs.getBoxNo());
            map.put("certificateKind",rmsRollWithBLOBs.getCertificateKind());
            map.put("deptId",rmsRollWithBLOBs.getDeptId());

            List<RmsRollWithBLOBs> RmsRollWithBLOBs = rmsRollMapper.query(map);
            json.setTotleNum(pageParams.getTotal());
            json.setObj(RmsRollWithBLOBs);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }


    @Override
    public ToJson<RmsRollWithBLOBs> selectById(Integer rollId) {
        ToJson<RmsRollWithBLOBs> json = new ToJson<RmsRollWithBLOBs>();
        try{
            RmsRollWithBLOBs rmsRollWithBLOBs = rmsRollMapper.selectById(rollId);
            if(!StringUtils.checkNull(rmsRollWithBLOBs.getManager())){
                Users usersByuserId = usersMapper.getUsersByuserId(rmsRollWithBLOBs.getManager());
                rmsRollWithBLOBs.setManagerName(usersByuserId.getUserName());
            }
            json.setObject(rmsRollWithBLOBs);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    @Override
    public ToJson<RmsRollWithBLOBs> selectAll() {
        ToJson<RmsRollWithBLOBs> json = new ToJson<RmsRollWithBLOBs>();
        try{
            List<RmsRollWithBLOBs> rmsRollWithBLOBs = rmsRollMapper.selectAll();
            json.setObj(rmsRollWithBLOBs);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }




}
